package com.java.product.controller;

import com.java.param.ProductCollectParam;
import com.java.param.ProductIdParam;
import com.java.pojo.Product;
import com.java.product.service.ProductService;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductCartController {
    @Autowired
    private ProductService productService;

    @PostMapping("/cart/detail")
    public Product detail(@RequestBody @Validated ProductIdParam productIdParam,BindingResult result){
        if(result.hasErrors()){
            return null;
        }
        R detail = productService.detail(productIdParam);
        Product product = (Product) detail.getData();
        return product;
    }
    @PostMapping("/cart/list")
    public List<Product> cartList(@RequestBody @Validated ProductCollectParam productCollectParam, BindingResult result){
        if(result.hasErrors()){
            return new ArrayList<Product>();
        }
        R ids = productService.Ids(productCollectParam.getProductIds());
        List<Product> products = (List<Product>) ids.getData();
        return products;
    }

}
